package com.gc.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import com.gc.business.domain.InventoryInfo;
import com.gc.business.domain.InventoryPrice;
import com.gc.business.service.IInventoryInfoService;
import com.gc.business.service.IInventoryPriceService;
import com.gc.business.service.IInventoryRecordService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 库存管理 控制层
 * 
 * @author hruiyan
 * @date 2020-11-10
 */
@Slf4j
@Controller
@RequestMapping("/business/inventoryInfo")
public class InventoryInfoController extends BaseController {
    private String prefix = "business/inventoryInfo";
	
	private final IInventoryInfoService inventoryInfoService;

	private final IInventoryPriceService inventoryPriceService;

	private final IInventoryRecordService inventoryRecordService;

	private final ISysUserService userService;

	@Autowired
	public InventoryInfoController(IInventoryInfoService inventoryInfoService,
								   IInventoryPriceService inventoryPriceService,
								   IInventoryRecordService inventoryRecordService,
								   ISysUserService userService) {
		this.inventoryInfoService = inventoryInfoService;
		this.inventoryPriceService = inventoryPriceService;
		this.inventoryRecordService = inventoryRecordService;
		this.userService = userService;

	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:inventoryInfo:view")
	@GetMapping
	public String inventoryInfo(){
	    return prefix + "/inventoryInfo";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap){
		mmap.put("orderNo", "LB".concat(String.valueOf(IdUtils.getPrimaryKey())));
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{inventoryId}")
	public String edit(@PathVariable("inventoryId") Long inventoryId, ModelMap mmap){
		InventoryInfo inventoryInfo = inventoryInfoService.getById(inventoryId);
		mmap.put("inventoryInfo", inventoryInfo);
		return prefix + "/edit";
	}
	
	/**
	 * 查询库存管理列表
	 */
	@RequiresPermissions("business:inventoryInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(InventoryInfo inventoryInfo){
		Long attrId = getAttrId();
		Boolean ifCompany = userService.ifCompanyRole(attrId);
		if(!ifCompany){
			return Result.success(getDataTable(new ArrayList<InventoryInfo>()));
		}
		startPage();
		inventoryInfo.setAttrId(attrId);
        List<InventoryInfo> list = inventoryInfoService.list(inventoryInfo);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存库存管理
	 */
	@RequiresPermissions("business:inventoryInfo:add")
	@Log(title = "库存管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(InventoryInfo inventoryInfo){
		Long attrId = getAttrId();
		Boolean ifCompany = userService.ifCompanyRole(attrId);
		if(!ifCompany){
			return Result.success();
		}
		inventoryInfo.setItemAgent(ShiroUtils.getSysUser().getUserName());
		inventoryInfo.setAttrId(attrId);
		inventoryInfoService.save(inventoryInfo);
		return Result.success();
	}

	/**
	 * 修改保存库存管理
	 */
	@RequiresPermissions("business:inventoryInfo:edit")
	@Log(title = "库存管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(InventoryInfo inventoryInfo){
		//修改之前先获取最新的库存数和总金额
		InventoryInfo info = inventoryInfoService.getById(inventoryInfo);
		inventoryInfo.setItemSum(info.getItemSum());
		inventoryInfo.setItemNum(info.getItemNum());
		inventoryInfoService.updateById(inventoryInfo);
		return Result.success();
	}
	
	/**
	 * 删除库存管理
	 */
	@RequiresPermissions("business:inventoryInfo:remove")
	@Log(title = "库存管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result remove(String ids){
		if (StringUtils.isEmpty(ids)){
			return Result.failed("参数错误,ids为空");
		}
		Long[] longIds = Convert.toLongArray(ids);
		for (Long inventoryId : longIds) {
			InventoryPrice inventoryPrice = new InventoryPrice().setInventoryId(inventoryId);
			List<InventoryPrice> inventoryPriceList = inventoryPriceService.list(inventoryPrice);
			if (CollUtil.isEmpty(inventoryPriceList)){
				inventoryInfoService.removeById(inventoryId);
			}else {
				return Result.failed("本物品存在出入库,不得删除!!!");
			}
		}
		return Result.success();
	}

	/**
	 * 导出库存管理列表
	 */
	@RequiresPermissions("business:inventoryInfo:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(InventoryInfo inventoryInfo){
		List<InventoryInfo> list = inventoryInfoService.list(inventoryInfo);
		ExcelUtil<InventoryInfo> util = new ExcelUtil<>(InventoryInfo.class);
		util.exportExcel(list, "inventoryInfo");
		return Result.success();
	}
}
